package com.slink.xin.framework.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class CollectionUtil {
	public static boolean isEmpty(List<?> list) {
		return !isNotEmpty(list);
	}

	public static boolean isNotEmpty(List<?> list) {
		if (list != null && list.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除字符串数组中重复的
	 * @param stringArray
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String[] duplicatRemoval(String[] stringArray) {

		if (stringArray == null || stringArray.length == 0)
			return null;
		Set set = new HashSet();
		for (int i = 0; i < stringArray.length; i++) {
			set.add(stringArray[i]);
		}
		int size = set.size();
		String[] temp = new String[size];
		Iterator it = set.iterator();
		int i = 0;
		while (it.hasNext()) {
			temp[i] = (String) it.next();
			i++;
		}
		return temp;
	}
	
	/**
	 * 取ls和ls2交集
	 * @param ls
	 * @param ls2
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List intersect(List ls, List ls2) {
        List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
        Collections.copy(list, ls);
        list.retainAll(ls2);
        return list;
    }

	/**
	 * 取ls和ls2并集
	 * @param ls
	 * @param ls2
	 * @return
	 */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List union(List ls, List ls2) {
        List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
        Collections.copy(list, ls);
       list.addAll(ls2);
        return list;
    }
    
	public static void main(String[] args) {
		String[] temp = { "chenkai", "chenkai", "taoming", "taoming" };
		CollectionUtil.duplicatRemoval(temp);
	}
}
